package com.itheima.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.SetmealDishesMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.*;

import com.itheima.service.SetmealService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Log4j2
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishesMapper setmealDishesMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加套餐
     *
     * @param setmeal
     * @return
     */
    @Override
    @Transactional
    public Result add(@RequestBody Setmeal setmeal) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("name", setmeal.getName());
        Setmeal one = setmealMapper.selectOne(wrapper);
        if (one != null) {
            return new Result(Code.OK, "菜品已存在", "{}");
        }
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());
        // TODO: 2022/10/18 数据没写进去
        setmealMapper.insert(setmeal);

        for (SetmealDishes sd : setmeal.getSetmealDishes()) {
            SetmealDishes dishes = new SetmealDishes();
            dishes.setCopies(sd.getCopies());
            dishes.setDishId(sd.getDishId());
            dishes.setName(sd.getName());
            dishes.setPrice(sd.getPrice());
            setmealDishesMapper.insert(dishes);
        }
        return new Result(Code.OK, "添加成功", null, "{}");

    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result getbyid(String id) {
        Setmeal byId = setmealMapper.selectById(id);
        //    查询套餐菜品表
        QueryWrapper<SetmealDishes> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);
        List<SetmealDishes> list = setmealDishesMapper.selectList(wrapper);
        //    重新装到套餐里
        byId.setSetmealDishes(list);
        return new Result(Code.OK, null, byId, "{}");
    }


    /**
     * 分页查询套餐
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result page(String name, Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper<>();
        if (!StrUtil.isBlank(name)) {
            wrapper.eq("name", name);
        }
        Page page1 = setmealMapper.selectPage(new Page<>(page, pageSize), wrapper);

        return new Result(Code.OK, null, page1, "{}");
    }

    /**
     * 删除多个删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result delect(String ids) {
        //    创建一个集合删除两个表
        List<String> list = new ArrayList<>();
        //    判断是否批量删除
        if (ids.contains(",")) {
            //    批量删除
            String[] split = ids.split(",");
            list = Arrays.asList(split);
            setmealMapper.deleteBatchIds(list);
            //    套餐菜品表删除
            for (String s : list) {
                //    创建对象
                QueryWrapper wrapper = new QueryWrapper<>();
                wrapper.eq("setmeal_id", s);
                setmealDishesMapper.delete(wrapper);
            }
        } else {
            //    单个删除
            setmealMapper.deleteById(ids);
            //    创建对象
            QueryWrapper wrapper1 = new QueryWrapper<>();
            wrapper1.eq("setmeal_id", ids);
            setmealDishesMapper.delete(wrapper1);
        }
        return new Result(Code.OK, null, "删除成功", "{}");
    }


    /**
     * 上下架
     *
     * @param status
     * @param ids
     * @return
     */
    @Override
    @Transactional
    // TODO: 2022/10/18 上下架 价格时间会变为0
    public Result auto(int status, String ids) {
        // 创建一个集合
        List<String> list;
        //        判断是否需要批量删除
        if (ids.contains(",")) {
            //    批量删除取出分隔符
            String[] split = ids.split(",");
            //    放到集合
            list = Arrays.asList(split);
            //    遍历 把list集合里的id一个个修改
            for (String s : list) {
                Setmeal setmeal = new Setmeal();
                setmeal.setId(s);
                setmeal.setStatus(status);
                setmealMapper.updateById(setmeal);
            }
        } else {
            //    单个修改
            Setmeal setmeal = new Setmeal();
            setmeal.setId(ids);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }
        return new Result(Code.OK, null, null, "{}");
    }


    /**
     * 修改套餐
     */
    @Override
    @Transactional
    public Result update( Setmeal setmeal) {
        // 更新套餐信息
       Category byId = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(byId.getName());
        setmealMapper.updateById(setmeal);

        //    更新套餐菜品表
        //    根据id删除对应菜品表信息
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", setmeal.getId());
        setmealDishesMapper.delete(wrapper);
        //    重新添加
        List<SetmealDishes> list = setmeal.getSetmealDishes();
        SetmealDishes dishes = new SetmealDishes();
        for (SetmealDishes sd : list) {
            sd.setSetmealId(setmeal.getId());
        }
        setmealDishesMapper.insert(dishes);
        return new Result(Code.OK, null, "修改成功", "{}");
    }


    /**
     * 移动端套餐列表
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public Result checkList(String categoryId, String status) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
     wrapper.eq("category_id", categoryId);
     wrapper.eq("status", status);
        //查询套餐表
        List<Setmeal> setmeals = setmealMapper.selectList(wrapper);

        return new Result(Code.OK, setmeals, "{}");
    }


    /**
     * 移动端套餐详情？？菜品详情
     * @param id
     * @return
     */
    @Override
    public Result checkDetail(String id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("setmeal_id", id);
        List<SetmealDishes> list = setmealDishesMapper.selectList(wrapper);

        return new Result(Code.OK, list, "{}");
    }
}
